package com.douqu.game.serverManager;

import com.bean.core.util.HttpJsonUtils;
import com.douqu.game.MenuManager;
import com.douqu.game.configManager.ServerUtils;
import com.douqu.game.serverManager.ServerManager;

import javax.swing.*;
import java.awt.*;
import java.util.Properties;

/**
 * @author: mriacle.
 * Description:
 * Date: 2018/4/28 0028 上午 11:01
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class ServerColumn {

    private int number;

    private String name;

    private JLabel jLabel;

    private JButton stop,mainb,battleb;

    private  Font font18 = new Font("宋体", 0, 18);

    Properties properties;
    /**
     *
     * @param number
     * @param y
     * @param container
     * @param properties
     * @return
     */
    public boolean init (int number,int y,Container container,Properties properties, MenuManager manager){

        this.number = number;
        this.properties = properties;
        name = properties.get(ServerManager.SERVER_NAME + number).toString();

        jLabel = new JLabel();
        jLabel.setBounds(20,y + 10,120,30);
        jLabel.setFont(font18);
        jLabel.setForeground(new Color(0, 0, 0));
        jLabel.setText("<html><font color='white'>" +
                name +
                "</font></html>");
        container.add(jLabel);

        mainb = new JButton("启动主服");
        mainb.setBounds(180, y + 10, 140, 30);
        mainb.setFont(font18);
        container.add(mainb);

        battleb = new JButton("启动战斗服");
        battleb.setBounds(180 + 140 + 10, y + 10, 140, 30);
        battleb.setFont(font18);
        container.add(battleb);

        stop = new JButton("停止");
        stop.setBounds(180 + 140 + 10 + 140 + 10, y + 10, 140, 30);
        stop.setFont(font18);
        container.add(stop);

        stop.addActionListener((event) -> {
            stop(manager);
        });
        mainb.addActionListener((event) -> {
            startMain(manager);
        });
        battleb.addActionListener((event)->{
            startBattle(manager);
        });

        return true;
    }

    public void stop(MenuManager manager){
        manager.println("开始停止服务" + name + "...");
        HttpJsonUtils.httpGet(properties.get(ServerManager.SERVER_STOP + number).toString());
        manager.println("停止服务" + name + "完成");
    }

    public void startMain(MenuManager manager){
        //开启主服务器
        manager.println("开始启动主服务器" + name + "...");
        if(!ServerUtils.run_cmd(properties.get(ServerManager.SERVER_MAIN_BAT + number).toString())){
            JOptionPane.showMessageDialog(manager.getMainFrame(), "主服务器" + name + "启动失败!");
        }else{
            manager.println("启动主服务器" + name + "成功");
        }
    }

    public void startBattle(MenuManager manager){
        //开启战斗服务器
        manager.println("开启战斗服务器" + name + "...");
        if(!ServerUtils.run_cmd(properties.get(ServerManager.SERVER_BATTLE_BAT + number).toString())){
            JOptionPane.showMessageDialog(manager.getMainFrame(), "战斗服务器" + name +"启动失败!");
        }else{
            manager.println("启动战斗服务器" + name + "成功");
        }
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
